package com.ruochen.recipe.customer.sectione;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruochen.common.adapter.RecyclerViewHolder;
import com.ruochen.common.listener.NoDoubleClickListener;

import java.util.List;

public abstract class SectionedExpandableGridAdapter<SG extends SectionGroup, SC extends SectionChild> extends RecyclerView.Adapter<RecyclerViewHolder> {

    //data array
    private List<Object> mDataArrayList;

    //context
    private final Context mContext;

    //listeners
    private final ItemClickListener mItemClickListener;
    private final SectionStateChangeListener mSectionStateChangeListener;

    private int groupLayoutRes;
    private int childLayoutRes;

    public SectionedExpandableGridAdapter(Context context, List<Object> dataArrayList,
                                          int groupLayoutRes,
                                          int childLayoutRes,
                                          final GridLayoutManager gridLayoutManager,
                                          ItemClickListener itemClickListener,
                                          SectionStateChangeListener sectionStateChangeListener) {
        mContext = context;
        this.groupLayoutRes = groupLayoutRes;
        this.childLayoutRes = childLayoutRes;
        mItemClickListener = itemClickListener;
        mSectionStateChangeListener = sectionStateChangeListener;
        mDataArrayList = dataArrayList;

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return isSection(position) ? gridLayoutManager.getSpanCount() : 1;
            }
        });
    }

    private boolean isSection(int position) {
        return mDataArrayList.get(position) instanceof SectionGroup;
    }

    @Override
    public int getItemCount() {
        return mDataArrayList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return isSection(position) ? 0 : 1;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(mContext, LayoutInflater.from(mContext).inflate(viewType == 0 ? groupLayoutRes : childLayoutRes, parent, false));
    }

    public abstract void convertGroup(RecyclerViewHolder holder, SG item, int position);

    public abstract void convertChild(RecyclerViewHolder holder, SC item, int position);

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        Object item = mDataArrayList.get(position);
        if (getItemViewType(position) == 0) {
            holder.getConvertView().setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View view) {
                    mItemClickListener.onItemGroupClick((SG) item, position);
                }
            });
            convertGroup(holder, (SG) item, position);
        } else {
            holder.getConvertView().setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View view) {
                    mItemClickListener.onItemChildClick((SC) item, position);
                }
            });
            convertChild(holder, (SC) item, position);
        }
    }

}
